package com.uobgroup.ews.uam.core.dao.ldap;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;

import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.SearchExecutor;

public class CustomSearchExecutor implements SearchExecutor {

   private String filter;

   private String baseDN;

   private String[] paras;

   public void setFilter(String filter) {
      this.filter = filter;
   }

   public void setBaseDN(String baseDN) {
      this.baseDN = baseDN;
   }

   public void setParas(String[] paras) {
      this.paras = paras;
   }

   @SuppressWarnings("rawtypes")
   public NamingEnumeration executeSearch(DirContext ctx) throws NamingException {
      DistinguishedName ctxBaseDn = new DistinguishedName(baseDN);
      SearchControls ctls = new SearchControls();
      ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
      ctls.setReturningObjFlag(false);
      ctls.setReturningAttributes(null);
      NamingEnumeration namingEnumeration = null;
      if (paras == null) {
         namingEnumeration = ctx.search(ctxBaseDn, filter, ctls);
      }
      else {
         namingEnumeration = ctx.search(ctxBaseDn, filter, paras, ctls);
      }
      return namingEnumeration;
   }

}
